/**
 *	视图列表数据JQGRID
 */

var loadViewData = function(_this, opts) {
    window.opts = opts;
    var winHeight = initWinHeight(opts.height);
    _this.jqGrid({
        datatype: function(postData){
            postData.rows = eval(postData.rows);
            if(opts.searchFastForm != undefined){
                var searchJson = $("#" + opts.searchFastForm + "").serializeObject();
                postData = $.toJSON($.extend( {}, postData, searchJson));
            }else{
                postData = $.toJSON(postData);
            }
            //alert(postData);
            $.post(opts.viewUrl,postData,function(rs){
                var gridData = _this[0];
                gridData.addJSONData(rs);
            });
        },
        height:winHeight,
        colNames:opts.colNames,
        colModel:opts.colModels,
        sortname: opts.sortName,
        sortorder: opts.sortOrder,
        multiselect:opts.multiSelect,
        multiboxonly:opts.multiBoxOnly,
        viewrecords: true,
        rownumbers:true,
        autowidth:true,
        ondblClickRow:function(rowid){
           //detailClick(rowid,opts);
        },
        pager: '#gridPager',
        rowNum:15,
        rowList:[10,15,20,25,30],
        jsonReader:{
            root:"list",
            total: "totalPage", page: "currPage", records: "totalRecord",
            repeatitems:false
        }
    });
    if(opts.operateState){
        _this.jqGrid('setGridParam',{
            gridComplete:function(){
                var ids=_this.jqGrid('getDataIDs');
                $.each(ids,function(key,item){
                    var operator = "";
                    var rowData = _this.jqGrid("getRowData", item);
                    $.each(opts.operate,function(i,operate){
                        if(operate == "modify"){
                            operator += "<a href=\"javascript:modifyClick('"+rowData.id+"');\" style=\"color:#ff9200\">修改</a>";
                        }
                        if(operate == "read"){
                            operator += "<a href=\"javascript:viewClick('"+rowData.id+"');\" style=\"color:#ff9200\">查看</a>";
                        }
                        if(operate == "delete"){
                            operator += "&nbsp;&nbsp;|&nbsp;&nbsp;<a href=\"javascript:deleteClick('"+rowData.id+"');\" style=\"color:#ff9200\">删除</a>";
                        }
                        if(operate == "reply"){
                            operator += "<a href=\"javascript:replyClick('"+rowData.id+"');\" style=\"color:#ff9200\">回复</a>";
                        }
                        if(operate == "reg"){
                            operator += "<a href=\"javascript:regClick('"+rowData.id+"');\" style=\"color:#ff9200\">认证</a>";
                        }
                    });
                    _this.jqGrid("setRowData", item, { operator: operator});
                });
            }
        }).trigger("reloadGrid");
    }
};

/**
 * 加载消息列表视图
 * @param optionsList 封装的参数对象
 * @param msgList 消息集合
 * from wxl
 */
var loadViewMsgList = function(optionsList,msgList){
    if(optionsList.page==1){//如果是第一次加载数据，那么情况该div的数据
        $("#wechat-msg").empty();
    }
    var findUser = function(userId){
        var optionsNew = {
            objectUrl:optionsList.rootPath+"/wechat/user/DataByOpenId",
            formId:userId
        };
        var user = $("").loadObjectData(optionsNew);
        return user;
    };
    var showMsgStatus = function(msgStatus){
        if(msgStatus=="0"){
            return "未回复";
        }else{
            return "已回复";
        }
    }
    var array=eval(msgList);
    $.each(array,function(i,o){
        var user = null;
        if((o.sendUser).length==28){
            user = findUser(o.sendUser);
            $("#wechat-msg").append("<div id='wechat-msg"+ o.id+"' class='wechat-msg'><div class='left'><img src='"+user.headImg+"' style='border-radius:50%;height:50px;width:50px;margin:3px'></div>" +
                "<div class='right'><img  id='imgSend"+ o.id+"' style='margin: 2px 30px 0px 0px;float:right;' name='btnSaveOrModify' src='"+optionsList.rootPath+"/skin/default/images/button/open.png' value='展开' /></div>" +
                "<div class='center'>" +
                "    <div>" +
                "    <h4 style='float: left;'>"+o.sendUserName+"</h4><h5 style='float: right;'>"+ getTimeGap(o.createTime)+"</h5><h5 style='float: right;margin-right: 20px;'>"+showMsgStatus(o.msgStatus)+"</h5><br><p>"+ o.msgContent+"</p></div>" +
                "    </div>" +
                "<div class='bottom' id='bottom"+o.id+"'><hr class='hr-light'>" +
                "   <form id='form"+ o.id+"' action='' method='post'>"+
                "   <div>" +
                "       <div style='border: 1px solid #bac7d2;height:160px;width:100%;margin-top: 5px;'>" +
                "         <div style='height: 80%;width: 100%;'>" +
                "         <textarea  name='msgContent' value='' style='height:100%;width: 100%;BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BORDER-BOTTOM-STYLE: none;'  require='NotNull'/>" +
                "         </div>" +
                "       </div>" +
                "       <div style='height: 44px;width:100%;margin-top: 1px;>" +
                "          <div style='float: right;'>" +
                "            <input type='hidden' id='id' name='id' value='"+ o.id+"."+o.msgContent+"' />"+
                "            <input type='hidden' name='sendUser' value='"+o.sendUser+"'>" +
                "            <input type='hidden' name='receiveUser' value='"+ o.receiveUser+"'>"+
                "            <input type='button' id='btnSave"+ o.id+"' style='border: azure;margin: 2px 10px 0px 0px;' name='btnSaveOrModify' class='btn_expand btn_send' value='发送' /> " +
                "            <input type='button' id='btnReturn"+ o.id+"' style='border: azure;margin: 2px 10px 0px 0px;' name='btnSaveOrModify' class='btn_expand btn_return' value='收起' /> " +
                "           </div>" +
                "       </div>    " +
                "   </div>" +
                "   </form> " +
                "</div></div>");
            var options = {
                submitUrl:"Save.do",
                returnUrl:optionsList.rootPath+"/wechat/msg/editor.do?id="+o.id+"&page=1&row=5",
                submitBtn:"btnSave"+ o.id,
                backBtn:"btnBack",
                openState:false,
                formId:id
            };
            $("#form"+ o.id).loadEditData(options);
        }else{
            $("#wechat-msg").append("<div class='wechat-msg'><div class='left'><img src='"+optionsList.rootPath+"/skin/default/images/main/kf.jpg' style='border-radius:50%;height:50px;width:50px;margin:3px'></div>" +
                "<div class='right'></div>" +
                "<div class='center'>" +
                "    <div>" +
                "    <h4 style='float: left;'>"+o.sendUserName+"</h4><h5 style='float: right;margin-right: 30px;'>"+ getTimeGap(o.createTime)+"</h5><br><p>"+ o.msgContent+"</p>" +
                "    </div>" +
                "</div></div>");
        };
        $("#imgSend"+ o.id).mouseover(function(){
            $("#imgSend"+ o.id).attr("src",optionsList.rootPath+"/skin/default/images/button/open_hover.png");
        });
        $("#imgSend"+ o.id).mouseout(function(){
            $("#imgSend"+ o.id).attr("src",optionsList.rootPath+"/skin/default/images/button/open.png");
        });
        $("#imgSend"+ o.id).toggle(function(){
            $("#wechat-msg"+ o.id).removeClass("wechat-msg");
            $("#wechat-msg"+ o.id).addClass("wechat-msg-large");
            $("#bottom"+ o.id).css("display","block");
        },function(){
            $("#wechat-msg"+ o.id).removeClass("wechat-msg-large");
            $("#wechat-msg"+ o.id).addClass("wechat-msg");
            $("#bottom"+ o.id).css("display","none");
        });
        $("#btnReturn"+ o.id).click(function(){
            $("#wechat-msg"+ o.id).removeClass("wechat-msg-large");
            $("#wechat-msg"+ o.id).addClass("wechat-msg");
            $("#bottom"+ o.id).css("display","none");
        });

    });
}